package com.zz.chenko.task.web;

import org.apache.commons.lang3.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zz.chenko.task.web.jobs.CollectInfoJob;

/**
 * @ClassName: TaskController
 * @Description: 定时任务
 * @author zhong
 * @date 2016年12月11日
 */
@Controller
@RequestMapping(value = "/task")
public class TaskController {

    private static Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Autowired
    SchedulerFactoryBean schedulerFactory;

    /**
     * 启动Job
     * 
     * @param name job名和trigger名
     * @param groupName 组名
     * @param cron 定时表达式
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/start", method = RequestMethod.POST)
    public void start(String name, String groupName, String cron) throws Exception {
        if (StringUtils.isBlank(name)) {
            return;
        }
        if (StringUtils.isBlank(groupName)) {
            groupName = PkgConst.DEFAULT_GROUP;
        }
        logger.info("------- 初始化调度 开始 ---------------");
        Scheduler sched = schedulerFactory.getScheduler();
        logger.info("------- 初始化调度 完成 -----------");

        logger.info("------- 调度-JOB-触发器 启动开始  -------------------");
        JobDetail collectInfoJob = JobBuilder.newJob(CollectInfoJob.class).withIdentity(name, groupName).build();

        Trigger collectInfoTrigger = TriggerBuilder.newTrigger().withIdentity(name, groupName)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
        sched.scheduleJob(collectInfoJob, collectInfoTrigger);
        sched.start();

        logger.info("------- 调度-JOB-触发器 启动完成 -----------------");
    }

    /**
     * 移除Job
     * 
     * @param name job名和trigger名
     * @param groupName 组名
     * @throws SchedulerException
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public void removeJob(String name, String groupName) throws SchedulerException {
        if (StringUtils.isBlank(name)) {
            return;
        }
        if (StringUtils.isBlank(groupName)) {
            groupName = PkgConst.DEFAULT_GROUP;
        }
        Scheduler sched = schedulerFactory.getScheduler();
        JobKey jobKey = new JobKey(name, groupName);
        TriggerKey triggerKey = new TriggerKey(name, groupName);
        logger.info("-------------- 停止触发器  开始 --------------");
        sched.pauseTrigger(triggerKey);// 停止触发器
        sched.pauseJob(jobKey);
        logger.info("-------------- 停止触发器 成功 --------------");
        logger.info("-------------- 移除触发器  开始 --------------");
        sched.unscheduleJob(triggerKey);// 移除触发器
        logger.info("-------------- 移除触发器  成功 --------------");
        sched.deleteJob(jobKey);// 删除任务
        logger.info("-------------- job删除成功 --------------");
    }
}
